#region OpenNETCF Copyright Information
/*
Copyright (c) 2007 OpenNETCF Consulting, LLC

Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial 
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using OpenNETCF.WindowsMobile.Ink;

namespace InkControlDemo
{
    public partial class Form1 : Form
    {
        private Color penColor = Color.Black;
        private IC_PENTYPE penType = IC_PENTYPE.NORMAL;
        public Form1()
        {
            InitializeComponent();
            inkCanvas1.NotificationMask = ICMASK.STROKEADDED | ICMASK.STROKEDELETED;
        }

        private void rbInk_CheckedChanged(object sender, EventArgs e)
        {
            if ( (sender as RadioButton).Checked )
                inkCanvas1.Mode = IC_EDITINGMODE.INK;
        }

        private void rbSelect_CheckedChanged(object sender, EventArgs e)
        {
            if ((sender as RadioButton).Checked)
                inkCanvas1.Mode = IC_EDITINGMODE.SELECT;
        }

        private void rbDelete_CheckedChanged(object sender, EventArgs e)
        {
            if ((sender as RadioButton).Checked)
                inkCanvas1.Mode = IC_EDITINGMODE.DELETE;
        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void mnuSave_Click(object sender, EventArgs e)
        {
            using (SaveFileDialog fd = new SaveFileDialog())
            {
                fd.Filter = "Ink files (*.isf)|*.isf|All files (*.*)|*.*";
                if (fd.ShowDialog() == DialogResult.OK)
                {
                    byte[] data = (byte[])inkCanvas1.GetInkData(IC_INKENCODING.BINARY);
                    FileStream stm = File.OpenWrite(fd.FileName);
                    stm.Write(data, 0, data.Length);
                    stm.Close();
                }
            }
        }

        private void mnuLoad_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog fd = new OpenFileDialog())
            {
                fd.Filter = "Ink files (*.isf)|*.isf|All files (*.*)|*.*";
                if (fd.ShowDialog() == DialogResult.OK)
                {
                    FileStream stm = File.OpenRead(fd.FileName);
                    byte[] data = new byte[(int)stm.Length];
                    stm.Read(data, 0, data.Length);
                    stm.Close();
                    inkCanvas1.LoadInkData(data);
                    pbPreview.Image = inkCanvas1.GetInkDataAsBitmap();
                }
            }
        }

        private void mnuClear_Click(object sender, EventArgs e)
        {
            inkCanvas1.ClearCanvas();
            inkCanvas1.EraseAllInk();
            pbPreview.Image = inkCanvas1.GetInkDataAsBitmap();
        }

        private void mnuChangeColor_Click(object sender, EventArgs e)
        {
        }

        private Color SelectColor(Color prev)
        {
            using (OpenNETCF.Windows.Forms.ColorDialog dlg = new OpenNETCF.Windows.Forms.ColorDialog())
            {
                dlg.AllowFullOpen = true;
                dlg.Color = prev;
                if (dlg.ShowDialog() == DialogResult.OK)
                    return dlg.Color;
                else
                    return prev;
            }
        }

        private void btnCanvasColor_Click(object sender, EventArgs e)
        {
            Color clr = SelectColor(Color.White);
            inkCanvas1.SetCanvasColor(clr);
        }

        private void btnBackColor_Click(object sender, EventArgs e)
        {
            Color clr = SelectColor(inkCanvas1.BackColor);
            inkCanvas1.BackColor = clr;
        }

        private void btnPenColor_Click(object sender, EventArgs e)
        {
            penColor = SelectColor(penColor);
            inkCanvas1.SetPenStyle(trackBar1.Value, penColor, penType);
        }

        private void inkControl1_StrokeAdded(object sender, StrokeEventArgs e)
        {
            pbPreview.Image = inkCanvas1.GetInkDataAsBitmap();
            lblReco.Text = inkCanvas1.RecognizedText;
        }

        private void inkControl1_StrokeDeleted(object sender, StrokeEventArgs e)
        {
            pbPreview.Image = inkCanvas1.GetInkDataAsBitmap();
        }

        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            inkCanvas1.SetPenStyle((float)trackBar1.Value * 100, penColor, penType);
        }
    }
}